//
//  TSAccountRecordTableViewCell.swift
//  TSProfitProject
//
//  Created by TMS on 2019/11/7.
//  Copyright © 2019 TMS. All rights reserved.
//

import UIKit
import HandyJSON

protocol TSAccountRecordTableViewCellDelegate : NSObjectProtocol {
    func didClickRevoke(model:TSRecordModel)
}

struct TSRecordModel : HandyJSON {
    
    var addtime : Int64 = 0
    var orderno = 0               // 订单号
    var money = 0                 // 金额
    var type : TSRecordType = TSRecordType.unknownType
    
    var four = -1
    var id = -1
    var jianghao : Int = 0        // 开奖号码
    var jizhu = -1
    var one = 0
    var qihao = 0                 // 期号
    var qufen = 2                 // 类型 1 -- 幸运 2 -- pk
    var shouyi : Double = 0
    var status = -1               // 0 -- 未开奖 1 -- 中奖 2 -- 未中奖
    var three = 0
    var two = 0
    var opentime : Int64 = 0
    var yingkui : String?
    
    var createtime : Int64 = 0
    var mome : String?
    var shenhe: Int = -1          // 0 未审核 1已通过 2未通过
}

class TSAccountRecordTableViewCell: UITableViewCell {
    
    weak var delegate : TSAccountRecordTableViewCellDelegate? = nil
    var numLabel : UILabel!
    var cashLabel : UILabel!
    var dateLabel : UILabel!
    var stateLabel : UILabel!
    var revokeBtn : UIButton!
    var line : UIView!
    var model : TSRecordModel? = nil
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        configViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func revokeAction() {
        if self.delegate != nil {
            self.delegate?.didClickRevoke(model: self.model!)
        }
    }
    
    func setupCell(model:TSRecordModel, last:Bool) {
        
        self.model = model
        
        numLabel.attributedText = createAttributedStr(str: "单号：\(model.orderno)", attributedStr: "\(model.orderno)", textColor: "#282828", fontSize: 12, attributedColor: "#0099F9")
        
        if self.model?.type == TSRecordType.investType { // 投注记录
            stateLabel.text = model.yingkui
            stateLabel.isHidden = model.status == 0
            revokeBtn.isHidden = model.status != 0
            cashLabel.text = model.qufen == 1 ? "幸运飞艇" : "北京赛车"
            let dateStr = Date.timeStampToString(timeStamp: model.addtime, outputFormatter: "MM月dd日 HH:mm")
            dateLabel.text = dateStr+" 第\(model.qihao)期"
            line.isHidden = last
            
            // FIXME:stateLabel的字体颜色
            if model.status == 1 {
                stateLabel.textColor = BASE_RED_COLOR
            } else {
                stateLabel.textColor = UIColor.black
            }
            
        } else if self.model?.type == TSRecordType.rechargeType || self.model?.type == TSRecordType.withdrawType {

            let str = self.model?.type == TSRecordType.rechargeType ? "充值金额：\(model.money)" : (model.mome ?? "提现金额")+"：\(model.money)"
            cashLabel.attributedText = createAttributedStr(str: str, attributedStr: "\(model.money)", textColor: "#282828", fontSize: 14, attributedColor: "#FF0000")
            dateLabel.text = Date.timeStampToString(timeStamp: model.createtime)
            switch model.shenhe {
            case 0:
                stateLabel.text = "未审核"
            case 1:
                stateLabel.text = "已通过"
            case 2:
                stateLabel.text = "未通过"
            default:
                break
            }
            
        } else {
            
            cashLabel.text = "账变类型："+(model.mome ?? "暂无记录")
            dateLabel.text = Date.timeStampToString(timeStamp: model.createtime)
            stateLabel.text = "\(model.money)"
        }
        
        
    }
    
    func configViews() {
        
        numLabel = UILabel.init()
        numLabel.attributedText = createAttributedStr(str: "单号：0", attributedStr: "0", textColor: "#292929", fontSize: 12, attributedColor: "#4077FB")
        contentView.addSubview(numLabel)
        numLabel.snp.makeConstraints { (make) in
            make.left.equalTo(12)
            make.top.equalTo(10)
        }
        
        cashLabel = UILabel.init()
        cashLabel.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        cashLabel.attributedText = createAttributedStr(str: "提现金额：0", attributedStr: "0", textColor: "#292929", fontSize: 14, attributedColor: "#FF0000")
        contentView.addSubview(cashLabel)
        cashLabel.snp.makeConstraints { (make) in
            make.left.equalTo(numLabel)
            make.top.equalTo(numLabel.snp_bottom).offset(10)
        }
        
        stateLabel = UILabel.init()
        stateLabel.textAlignment = .right
        stateLabel.text = "0"
        stateLabel.font = UIFont.systemFont(ofSize: 16, weight: .medium)
        contentView.addSubview(stateLabel)
        stateLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(cashLabel)
            make.left.equalTo(cashLabel.snp_right).offset(10)
            make.right.equalToSuperview().inset(12)
        }
        
        revokeBtn = UIButton.init(type: .system)
        revokeBtn.setTitle("撤单", for: .normal)
        revokeBtn.setTitleColor(BASE_PURPLE_COLOR, for: .normal)
        revokeBtn.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .medium)
        revokeBtn.isHidden = true
        revokeBtn.addTarget(self, action: #selector(revokeAction), for: .touchUpInside)
        contentView.addSubview(revokeBtn)
        revokeBtn.snp.makeConstraints { (make) in
            make.centerY.equalTo(cashLabel)
            make.right.equalToSuperview().inset(12)
        }
        
        dateLabel = UILabel.init()
        dateLabel.attributedText = createAttributedStr(str: "申请时间：2019年3月23日 14:23", attributedStr: "", textColor: "#989898", fontSize: 12, attributedColor: "")
        contentView.addSubview(dateLabel)
        dateLabel.snp.makeConstraints { (make) in
            make.left.equalTo(numLabel)
            make.right.equalToSuperview().inset(10)
            make.top.equalTo(cashLabel.snp_bottom).offset(10)
            
            make.bottom.equalTo(-12)
        }
        
        line = UIView.init()
        line.backgroundColor = UIColor.hexadecimalColor(hexadecimal: "#F5F5F5")
        contentView.addSubview(line)
        line.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(1)
            
        }
    }
    
    func createAttributedStr(str:String, attributedStr:String, textColor:String, fontSize:CGFloat, attributedColor:String) -> NSAttributedString {
        let text = NSString(string: str)
        let attribute = NSMutableAttributedString.init(string: str)
        
        attribute.addAttributes([NSAttributedString.Key.foregroundColor:UIColor.hexadecimalColor(hexadecimal: textColor), NSAttributedString.Key.font:UIFont.systemFont(ofSize: fontSize, weight: .medium)], range: NSMakeRange(0, str.count))
        
        if attributedStr.count > 0 {
            attribute.addAttributes([NSAttributedString.Key.foregroundColor:UIColor.hexadecimalColor(hexadecimal: attributedColor)], range: NSMakeRange(str.count - attributedStr.count, attributedStr.count))
        }
        
        return attribute.copy() as! NSAttributedString
    }
    

}
